<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/sys-admin/index' }" style="size: 16px">
                <i class="el-icon-s-promotion"></i>
                后台管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>添加品牌</el-breadcrumb-item>
        </el-breadcrumb>

        <el-divider></el-divider>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name" ></el-input>
            </el-form-item>
            <el-form-item label="拼英" prop="pinyin">
                <el-input v-model="ruleForm.pinyin" ></el-input>
            </el-form-item>
            <el-form-item label="LOGO" prop="logo">
                <el-input v-model="ruleForm.logo" ></el-input>
            </el-form-item>
            <el-form-item label="简介" prop="description">
                <el-input v-model="ruleForm.description" ></el-input>
            </el-form-item>
            <el-form-item label="关键词列表" prop="keywords">
                <el-input v-model="ruleForm.keywords" ></el-input>
            </el-form-item>
            <el-form-item label="排序序号" prop="sort">
                <el-input v-model="ruleForm.sort" ></el-input>
            </el-form-item>
            <el-form-item label="是否启用" >
                <el-switch
                        v-model="ruleForm.enable"
                        active-color="#13ce66"
                        inactive-color="#999"
                        active-value="1"
                        inactive-value="0">
                </el-switch>
            </el-form-item >

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {

        data() {
            return {
                roleListOptions:'',
                ruleForm: {
                    name:'测试品牌1',
                    pinyin:'ceshipinpai1',
                    logo:'https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png',
                    description:'测试品牌简介',
                    keywords:'测试关键词1,测试关键词2,测试关键词3',
                    sort:'99',
                    enable:'1',
                },
                rules: {
                    name: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
                    ],
                    pinyin: [
                        { required: true, message: '请输入拼英', trigger: 'blur' },
                        { min: 3, max: 35, message: '长度在 3 到 15 个字符', trigger: 'blur' }
                    ],
                    logo: [
                        { required: true, message: '请输入Logo的url', trigger: 'blur' },
                        { min: 10, max: 255, message: '长度在 10 到 255 个字符', trigger: 'blur' }
                    ],
                    description: [
                        { required: true, message: '请输入简介', trigger: 'blur' },
                        { min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur' }
                    ],
                    sort: [
                        { required: true, message: '请输入排序', trigger: 'blur' },
                        { min: 1, max: 99, message: '范围1-99', trigger: 'blur' }
                    ],

                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url="http://localhost:9080/brands/add-new"
                        console.log('url='+url);

                        let fromData=this.qs.stringify(this.ruleForm);
                        console.log('fromData='+fromData);

                        this.axios
                            .create({
                                headers:{
                                    'Authorization':localStorage.getItem('jwt')
                                }
                            })
                            .post(url,fromData).then((response)=>{
                            let responseBody=response.data;
                            console.log(responseBody);
                            if(responseBody.state==20000){
                                this.$message({
                                    showClose: true,
                                    message: '品牌添加成功!',
                                    type: 'success'
                                });
                                this.resetForm(formName);
                            }else{
                                this.$message({
                                    showClose: true,
                                    message: responseBody.message,
                                    type: 'error'
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },

            resetForm(formName) {
                this.$refs[formName].resetFields();
            },


        },
        mounted() {

        }
    }

</script>

<style scoped>

</style>